﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LibrarySystem.Models;
using LibrarySystem.LibraryUtils.Entities;

namespace LibrarySystem.LibraryUtils.DAL.DAOs
{
    public class Library_SkippingDetails : Common
    {
        public List<SkippingDetailModel> GetDataList(int skippingId = 0, int startIndex = 0, int pageSize = 0, string sorting = null)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            IEnumerable<Library_SkippingDetail> querySkipDetails = _context.Library_SkippingDetails;
            //Filter
            if (skippingId > 0)
            {
                querySkipDetails = querySkipDetails.Where(sd => sd.Library_Skipping.Id == skippingId);
            }
            //Sorting
            if (string.IsNullOrEmpty(sorting) || sorting.Equals("SkippingID ASC"))
            {
                querySkipDetails = querySkipDetails.OrderBy(sd => sd.SkippingId);
            }
            else if (sorting.Equals("SkippingID DESC"))
            {
                querySkipDetails = querySkipDetails.OrderByDescending(sd => sd.SkippingId);
            }
            //
            List<Library_SkippingDetail> skipDetails = pageSize > 0 ?
                                                        querySkipDetails.Skip(startIndex).Take(pageSize).ToList()
                                                        : querySkipDetails.ToList();
            List<SkippingDetailModel> detailList = new List<SkippingDetailModel>();
            foreach (Library_SkippingDetail sd in skipDetails)
            {
                SkippingDetailModel data = new SkippingDetailModel();
                data.ID = sd.Id;
                data.SkippingID = sd.Library_Skipping.Id;
                data.BookID = sd.Library_Book.Id;
                data.Quantity = sd.Quantity;
                detailList.Add(data);
            }
            _context.Dispose();
            return detailList;
        }

        public SkippingDetailModel AddNewData(SkippingDetailModel data)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            Library_SkippingDetail skipDetails = new Library_SkippingDetail();
            //Assign Para
            skipDetails.SkippingId = data.SkippingID;
            skipDetails.BookId = data.BookID;
            skipDetails.Quantity = data.Quantity;
            //Insert
            _context.Library_SkippingDetails.InsertOnSubmit(skipDetails);
            _context.SubmitChanges();
            //Assign new ID to Model
            data.ID = skipDetails.Id;
            //
            _context.Dispose();
            return data;
        }

        public bool UpdateData(SkippingDetailModel data)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            Library_SkippingDetail skipDetails = _context.Library_SkippingDetails.Where(sd => sd.Id == data.ID).FirstOrDefault();
            //Assign Param
            skipDetails.BookId = data.BookID;
            skipDetails.Quantity = data.Quantity;
            //Update
            _context.SubmitChanges();
            //
            _context.Dispose();
            return true;
        }

        public bool DeleteData(int Id)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            Library_SkippingDetail skipDetails = _context.Library_SkippingDetails.Where(sd => sd.Id == Id).FirstOrDefault();
            //Delete
            _context.Library_SkippingDetails.DeleteOnSubmit(skipDetails);
            _context.SubmitChanges();
            //
            _context.Dispose();
            return true;
        }
    }
}